package com.hongyun.sdk.customer.notice.handle;

import com.alibaba.fastjson2.JSON;
import com.hongyun.sdk.common.HYResponse;
import com.hongyun.sdk.common.HongYunAudioStatus;
import com.hongyun.sdk.customer.request.HYOrderRefundRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
public class HYOrderNoticeHandle {

    /**
     * 获取核销状态变更单号
     *
     * @param request
     * @return
     * @throws IOException
     */
    public String writeOffNoticeRequestHandle(HttpServletRequest request) throws IOException {
        String originalOrderNo = request.getParameter("originalOrderNo");
        log.info("original order no:{}", originalOrderNo);
        return originalOrderNo;
    }

    /**
     * 核销状态返回
     *
     * @param response
     * @return
     * @throws IOException
     */
    public ResponseEntity writeOffNoticeResponseHandle(HYResponse response) throws IOException {
        return ResponseEntity.status(HttpStatus.OK).body(response);
    }


    /**
     * 获取退款结果
     *
     * @param request
     * @return
     * @throws IOException
     */
    public HYOrderRefundRequest refundNoticeRequestHandle(HttpServletRequest request) throws IOException {
        HYOrderRefundRequest refundRequest = new HYOrderRefundRequest();
        String originalOrderNo = request.getParameter("originalOrderNo");//String	是	鸿运订单号	鸿运主动发起
        refundRequest.setOriginalOrderNo(originalOrderNo);
        String auditStatus = request.getParameter("auditStatus");//Integer	是	状态	20=同意退款、30=拒绝退款
        if (auditStatus != null) {
            Integer _status = (Integer) Integer.parseInt(auditStatus);
            refundRequest.setAuditStatus(_status);
            if (_status == HongYunAudioStatus.synonymous.getStatus()) {
                refundRequest.setAudioStatus(HongYunAudioStatus.synonymous);
            } else if (_status == HongYunAudioStatus.refuse.getStatus()) {
                refundRequest.setAudioStatus(HongYunAudioStatus.refuse);
                String reason = request.getParameter("reason");//String	否	拒绝原因	auditStatus=30时，必须输入
                refundRequest.setReason(reason);
            }
        }
        return refundRequest;
    }

    /**
     * 获取退款结果相应处理
     *
     * @param response
     * @return
     * @throws IOException
     */
    public ResponseEntity refundOffNoticeResponseHandle(HYResponse response) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.forEach(response::put);
        return new ResponseEntity<>(JSON.toJSONString(response), headers, HttpStatus.OK);
    }
}
